package com.hqd.test.xml.custom;

import com.hqd.ch03.v7.config.BeanDefinition;
import com.hqd.ch03.v7.reader.xml.parse.BeanDefinitionDecorator;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.HashMap;
import java.util.Map;

public class HobbiesBeanDefinitionDecorator implements BeanDefinitionDecorator {

    private Map<String, String> parsePropertyValues(Element ele) {
        String hobbiesStr = ele.getAttribute("value");
        if (StringUtils.isNoneBlank(hobbiesStr)) {
            HashMap<String, String> map = new HashMap<>();
            map.put("hobbies", hobbiesStr);
            return map;
        }
        return null;
    }

    @Override
    public BeanDefinition decorate(Node node, BeanDefinition definition) {
        if (node instanceof Element
                && StringUtils.equalsIgnoreCase(node.getLocalName(), "hobbies")) {
            Element ele = (Element) node;
            Map<String, String> map = parsePropertyValues(ele);
            if (map != null) {
                definition.getProperties()
                        .addPropertyValues(map);
            }
        }
        return definition;
    }
}
